package dragon.www.fly.Utils;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.adjust.sdk.Adjust;
import com.adjust.sdk.AdjustAttribution;
import com.adjust.sdk.AdjustConfig;
import com.adjust.sdk.LogLevel;
import com.adjust.sdk.OnAttributionChangedListener;
import com.adjust.sdk.OnDeviceIdsRead;
import com.cash.lemoloan.statisticlib.Utils.PreferencesUtil;

public class AdjustUtil {

    public static void init(Application application) {
        String appToken = "yxyl32c9lvk0";
//        调试环境
//        String environment = AdjustConfig.ENVIRONMENT_SANDBOX;
//        LogLevel level = LogLevel.WARN;
//        生产环境
        String environment = AdjustConfig.ENVIRONMENT_PRODUCTION;
        LogLevel level = LogLevel.SUPRESS;
        Context context = application.getApplicationContext();
        if (context != null) {
            AdjustConfig adjustConfig = new AdjustConfig(context, appToken, environment);
            adjustConfig.setLogLevel(level);
            adjustConfig.setOnAttributionChangedListener(new OnAttributionChangedListener() {
                @Override
                public void onAttributionChanged(AdjustAttribution attribution) {
                    if (attribution != null) {
                        String adid = attribution.adid;
                        PreferencesUtil.putString("adid", adid);
                        String campaign = attribution.campaign;
                        PreferencesUtil.putString("campaign", campaign);
                        String network = attribution.network;
                        PreferencesUtil.putString("network", network);
                    }
                }
            });
            Adjust.onCreate(adjustConfig);
            application.registerActivityLifecycleCallbacks(new AdjustLifecycleCallbacks());
            Adjust.getGoogleAdId(context, new OnDeviceIdsRead() {
                @Override
                public void onGoogleAdIdRead(String googleAdId) {
                    if (!TextUtils.isEmpty(googleAdId)) {
                        PreferencesUtil.putString("googleAdId", googleAdId);
                    }
                }
            });
        }
    }

    public static void callAdjustAttribution() {
        //归因数据
        AdjustAttribution attribution = Adjust.getAttribution();
        if (attribution != null) {
            String adid = attribution.adid;
            PreferencesUtil.putString("adid", adid);
            String campaign = attribution.campaign;
            PreferencesUtil.putString("campaign", campaign);
            String network = attribution.network;
            PreferencesUtil.putString("network", network);
        }
    }

    private static final class AdjustLifecycleCallbacks implements Application.ActivityLifecycleCallbacks {
        @Override
        public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle bundle) {

        }

        @Override
        public void onActivityStarted(@NonNull Activity activity) {

        }

        @Override
        public void onActivityResumed(Activity activity) {
            Adjust.onResume();
        }

        @Override
        public void onActivityPaused(Activity activity) {
            Adjust.onPause();
        }

        @Override
        public void onActivityStopped(@NonNull Activity activity) {

        }

        @Override
        public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle bundle) {

        }

        @Override
        public void onActivityDestroyed(@NonNull Activity activity) {

        }
    }
}
